﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BKO.DataAccesss;
using BKO.DataAccesss.Repositories;
using BKO.Domain.Model;
using BKO.Domain.Repositories;
using BKO.Domain.Services;

namespace BKO.Services
{
	public class ConfigureService : IConfigureService
	{
		private readonly IRepository<IConfigure> _cfgRepository;

		public ConfigureService(string connectionString)
		{
			_cfgRepository = new ConfigureRepository(BKODataContextSingletone.GetInstance(connectionString));
		}

		public IConfigure GetConfigure()
		{
			return _cfgRepository.Get();
		}

		public IConfigure SaveConfigure(string sysAdmin, string organisation, string programName, string nr, string access = null)
		{
			IConfigure cfg = GetConfigure();

			if (cfg == null)
			{
				cfg = new Configure
					{
						SISADM = sysAdmin,
						ORG = organisation,
						Name_Prog = programName,
						NR = nr,
						access = access ?? string.Empty
					};
				_cfgRepository.Add(cfg);
			}
			else
			{
				cfg.SISADM = sysAdmin;
				cfg.ORG = organisation;
				cfg.Name_Prog = programName;
				cfg.NR = nr;
				if (!string.IsNullOrEmpty(access))
				{
					cfg.access = access;
				}
				_cfgRepository.Update();
			}

			return cfg;
		}
	}
}
